package com.jq.jqaicode.controller;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.jq.jqaicode.common.BaseResponse;
import com.jq.jqaicode.common.ResultUtils;
import com.jq.jqaicode.model.entity.User;
import com.jq.jqaicode.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    /**
     * 用户登录
     * @param userNo
     * @param pwd
     * @return
     */
    @RequestMapping("/doLogin")
    public BaseResponse<SaTokenInfo> doLogin(@RequestParam("userNo")String userNo,@RequestParam("pwd") String pwd) {
        SaTokenInfo saTokenInfo = userService.doLogin(userNo, pwd);
        if(saTokenInfo==null){
            ResultUtils.runError("账号或密码不对，登录失败");;
        }
        return ResultUtils.success(saTokenInfo);
    }

    /**
     * *查询用户信息
     * * * @param id
     * @return
     */
    @RequestMapping("/getUserInfo")
    public BaseResponse<User> doLogin() {
        User user = userService.getUserInfo(StpUtil.getLoginIdAsString());
        if(user==null){
            ResultUtils.runError("用户信息不存在");;
        }
        return ResultUtils.success(user);
    }


    /**
     * 用户退出
     */
    @RequestMapping("/logOut")
    public BaseResponse<?> logOut() {
        try {
            StpUtil.logout();
            return ResultUtils.success("已退出登录");
        } catch (Exception e) {
            return ResultUtils.runError("退出登录失败");
        }
    }
    
}
